<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<link rel="stylesheet" type="text/css" media="screen" href="css/smoothness/jquery-ui-1.9.0.custom.css" />	<!-- jqueryUI CSS -->
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />							<!-- jqGrid CSS -->
<link rel="stylesheet" type="text/css" media="screen" href="css/selfTemplate.css" />						<!-- 本專案自行定義 CSS -->

<script src="js/jquery-1.8.0.min.js" type="text/javascript"></script>										<!-- jQuery 核心 -->
<script src="js/grid.locale-tw.js" type="text/javascript"></script>											<!-- jqGrid 語系檔 -->
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>										<!-- jqGrid 核心 -->
<script src="js/jquery-ui-1.9.0.custom.min.js" type="text/javascript"></script>							<!-- jqueryUI 核心 -->
<script src="js/widgets.js" type="text/javascript"></script>												<!-- 本專案自行定義 JS -->

<title>維護部署小組</title>

<script type="text/javascript">

function checkDuplicated(grid) {
	var squadId = $("#squadid")[0].value;
	var ll = $("#detailTable_"+grid).jqGrid('getDataIDs');
	var isDuplicated = false;
	for(var i=0;i<ll.length;i++){
		if (ll[i] == squadId ) {
			isDuplicated = true;
		}
	}
	
	if (isDuplicated) {
		alert(squadId+"已經存在，部署小組ID不可重覆 !");
	} else {
		var squadName = $("#squadname")[0].value;
		var dataAdd = {"squadid":squadId,"squadname":squadName,"creator":"842297","createdDateTime":"FFFFFF"};
		var _iCount = $("#detailTable_"+grid).jqGrid('getGridParam', 'records');
		$("#detailTable_"+grid).jqGrid('addRowData', _iCount+1,dataAdd);
	} 
	return [];
}

function buildGrid(grid){
	$("#masterTable_"+grid).jqGrid({
		url:'QueryEnv',
		postData: {
			subSysId: function() { return grid; },
		},
		datatype: "json",
	   	colNames:['環境ID', '環境名稱','創建人員', '創建日期'],
	   	colModel:[
	   		{name:'envid',index:'envid',editable:true, edittype:"text",editrules:{required:true},editoptions: {size:20, maxlength: 20}},
	   		{name:'envname',index:'envname',editable:true, edittype:"text",
	   			editrules:{custom:true,required:true,custom_func:function(){
	   				$("#isNotDuplicated")[0].value = true;
	   				var envId = $("#envid")[0].value;
	   				var envName = $("#envname")[0].value;
	   				var tabIdx = $("#tabsDiv").tabs("option", "selected");
	   				
   					$("#bssId_form")[0].value = "MBMS";
   					$("#subSysId_form")[0].value = $("#tab_"+tabIdx).attr("subSys");
   					$("#envId_form")[0].value = envId;
   					$.post("IsDuplicated",$("#q_form").serializeArray(),function(res) {
   						if (res == "true") {
   							$("#isNotDuplicated")[0].value = false;	
   						}
   					},"TEXT");
   					var rr = $("#isNotDuplicated")[0].value;
					return [rr,"環境ID重複了，換一個!"];
	   			}	// custom_func 結尾
	   		}	//  editrules 結尾
	   		},
	   		{name:'creator',index:'creator'},
	   		{name:'createdDateTime',index:'createdDateTime'}
	   	],
	   	rowNum:10,
	   	pager: "#masterPager_"+grid,
		multiselect: false,
		rownumbers: true,
	   	viewrecords: true,	
		caption: "環境",
		onSelectRow: function(ids) {
			jQuery("#detailTable_"+grid).jqGrid("setCaption","部署小組所屬的環境--"+ids);
			jQuery("#detailTable_"+grid).jqGrid('setGridParam',{
				url:"QuerySquad",
				postData: {
					tarSys: function() { return "MBMS"; },
					tarSubSys: function() {
						return grid; 
					},
					tarEnv: function() { return ids; },
				}
			//});
			}).trigger('reloadGrid');
		},
		/**	This event is executed immediately after every server request. **/
		loadComplete: function(data) {
			jQuery("#detailTable_"+grid).jqGrid({
				datatype: "json",
				postData: {
					getDataType: function() { return "mapping"; },
				},
				colNames:["<input type='checkbox' onclick='checkBox(event)' />",'部署小組ID', '部署小組名稱','創建人員', '創建日期'],
			   	colModel:[
					{name:'my_checkbox',index:'my_checkbox', edittype:"checkbox",
					formatter: "checkbox", formatoptions: {disabled:false},width:40,align:"center" },
			   		{name:'squadid',index:'squadid',editable:true,editrules:{required:true}},
			   		{name:'squadname',index:'squadname',editable:true,editrules:{required:true}},
			   		{name:'creator',index:'creator'},
			   		{name:'createdDateTime',index:'createdDateTime'}
			   	],
			   	rowNum:10,
			   	loadonce:false,
			   	rowList:[10,20,30],
			   	pager: "#detailPager_"+grid,
				multiselect: false,
				rownumbers: true,
			   	viewrecords: true,
			   	editurl: "ManipulateGridSquad",
				caption:"部署小組所屬的環境"
			}).navGrid("#detailPager_"+grid,{add:false,edit:false,del:false}
			,{}
			,{}
			,{});		
		}
	}).jqGrid("navGrid","#masterPager_"+grid,{add:false,edit:false,del:false},
			{},{},{});
}

		

$(document).ready(function() {
	
	$("#getDataType")[0].value = "JSON";
	$.post("QuerySubSys",$("#q_form").serializeArray(),function(res) {
		$.each(res,function(i, tmp) {
			$("#tabsUl").append("<li><a id=\"tab_"+i+"\" no=\""+i+"\" subSys=\""+tmp.id+"\" href=\"#tab_"+tmp.id+"\">"+tmp.name+" ("+tmp.id+")</a></li>");
			$("#tabsDiv").append("<div id=\"tab_"+tmp.id+"\"><table id=\"masterTable_"+tmp.id+"\"></table><div id=\"masterPager_"+tmp.id+"\"></div><p><table id=\"detailTable_"+tmp.id+"\"></table><div id=\"detailPager_"+tmp.id+"\"></div>");
			buildGrid(tmp.id);
		});
		
		$("#tabsDiv").tabs();
	},"JSON");
	$("#getDataType")[0].value = "";
	
	$("#tabsUl").tabs("select", 2);
});
</script>

</head>
<body>
<div id="funcDisplay">
選擇部署小組
</div>
<form id="q_form">
	<input type="hidden" id="getDataType" name="getDataType" />
	<input type="hidden" id="bssId_form" name="bssId_form" />
	<input type="hidden" id="subSysId_form" name="subSysId_form" />
	<input type="hidden" id="envId_form" name="envId_form" />
</form>
<input id="subsyses" type="hidden" >
<input id="isNotDuplicated" type="hidden" >

<br/>

<div id="tabsDiv">
<ul id="tabsUl">
</ul>
</div>


</body>
</html>